<!DOCTYPE html>
<html class="ui-page-login">

	<head>
		<meta charset="utf-8">
		<meta http-equiv="Access-Control-Allow-Origin" content="*">
		<meta http-equiv="content-security-policy">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<title>掌上工单</title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/style.css" rel="stylesheet" />
		<link href="css/mui.picker.min.css" rel="stylesheet" />
		<link href="css/mui.poppicker.css" rel="stylesheet" />
		<style>
			.area {
				margin: 20px auto 0px auto;
			}
			.mui-input-group {
				/*margin-top: 10px;*/
			}
			.mui-input-group:first-child {
				margin-top: 20px;
			}
			.mui-input-group label {
				width: 22%;
			}
			.mui-input-row label~input,
			.mui-input-row label~select,
			.mui-input-row label~textarea {
				width: 78%;
			}
			.mui-checkbox input[type=checkbox],
			.mui-radio input[type=radio] {
				top: 6px;
			}
			.mui-content-padded {
				margin-top: 25px;
			}
			.mui-btn {
				padding: 10px;
			}
			.link-area {
				display: block;
				margin-top: 25px;
				text-align: center;
			}
			.spliter {
				color: #bbb;
				padding: 0px 8px;
			}
			.mui-bar{
				background-color: rgba(0,153,204,.9);
			}
			.mui-icon{
				color: #FFFFFF;
			}
		</style>

	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<h1 class="mui-title" style="color: #FFFFFF;">掌上工单</h1>
		</header>
		<div class="mui-content">
			<div style="height:140px;width:100%;background-image:url(images/flash1.jpg);background-position:center center;"></div>
			<form id='login-form' class="mui-input-group">
				
				<div class="mui-row">
					<button id='login_type' class="mui-btn mui-btn-block" type='button'>请选择登录方式</button>
				</div>
				
				<div class="mui-input-row">
					<label>账号</label>
					<input id='account' type="text" class="mui-input-clear mui-input" placeholder="请输入账号" value="">
				</div>
				
				<div class="mui-input-row">
					<label>密码</label>
					<input id='password' type="password" class="mui-input-clear mui-input" placeholder="请输入密码" value="">
				</div>
			</form>

			<div class="mui-content-padded">
				<button id="login" class="mui-btn mui-btn-block mui-btn-primary">登录</button>
				<div class="link-area mui-hidden">
					<a id='forgetPassword'>忘记密码</a>
				</div>
			</div>
		</div>
		<script src="js/mui.min.js"></script>
		
		<script src="js/mui.picker.min.js"></script>
		<script src="js/mui.poppicker.js"></script>
		<script src="js/mui.enterfocus.js"></script>
		<script type="text/javascript" src="js/md5.js" ></script>
		<script type="text/javascript" src="js/uuid.js" ></script>
		<script type="text/javascript" src="js/utils.js" ></script>
		<script src="js/login.js"></script>
		<script>
			
			(function($) {

			    var main,
			        backButtonPress = 0;
			
			    $.plusReady(function(){
			        main =  plus.android.runtimeMainActivity();
			    });
			
			    $.back = function() {
			        backButtonPress++;
			        if (backButtonPress > 1) {
			            //退出
			            //plus.runtime.quit();
			            //后台
			            main && main.moveTaskToBack(false);
			        } else {
			            plus.nativeUI.toast('再按一次返回桌面');
			        }
			        setTimeout(function() {
			            backButtonPress = 0;
			        }, 1000);
			        return false;
			    };
			
			}(mui));
			
		</script>
		<script>
			
			(function($, doc) {
				
				var loginType = 0 ;
				var roles = [{
						value: 1,
						text: '客服系统',
						url:URL_LOGIN
					}, {
						value: 2,
						text: '调度系统',
						url:EMC_URL_LOGIN
					}] ;
				$.init({
					statusBarBackground: '#f7f7f7'
				});
				var url =  URL_LOGIN ;
				$.plusReady(function() {
					//检查 "登录状态/锁屏状态" 结束
					var loginButton = doc.getElementById('login');
					var accountBox = doc.getElementById('account');
					var passwordBox = doc.getElementById('password');
					var forgetButton = doc.getElementById('forgetPassword');
					var loginTypeButton = doc.getElementById('login_type');
					var loginTypePicker = new $.PopPicker();
					var waiting = null ;
					var clientInfo = plus.push.getClientInfo();
					var user = xhz.getUserName() ;
					var text = '请选择登入方式' ;
					
					if(xhz.getUser()){
						if(1 == xhz.getUser().loginType){
							text = roles[0].text ;
							loginType = roles[0].value ;
							url =  roles[0].url ; ;
						}else if(2 == xhz.getUser().loginType){
							text = roles[1].text ;
							loginType = roles[1].value ;
							url =  roles[1].url ; ;
						}
					} 
					loginTypeButton.innerText = text ;
					console.debug( " user  " +text)
					loginType = xhz.getUser().loginType || 0 ;
					
					plus.screen.lockOrientation("portrait-primary");
					var toMain = function() {
						console.debug("登录类型" + loginType)
						
						var targetPage  = loginType*1 == 2 ? {id:'emc-task-search',page:'emc/task-search.html'} :{id:'index',page:'index.html'};
						plus.webview.currentWebview().parent().hide();
						$.openWindow({
							id: targetPage.id,
							url: targetPage.page,
							show: {
								aniShow: 'pop-in'
							},
							waiting: {
								autoShow: false
							}
						});
					};
					
					setTimeout(function() {
						plus.navigator.closeSplashscreen();
					}, 600);
					
					if(user && user.loginname){
						accountBox.value = user.loginname ;
					}
					
					loginButton.addEventListener('tap', function(event) {
						
//						if(!xhz.network()){
//							$.toast("亲，你不给我网络，我就罢工!", "提示");
//							return ;
//						}
						if(!loginType){
							$.toast("亲，请选择登录方式!", "提示");
							return  ;
						}
						waiting = plus.nativeUI.showWaiting() ;
						var loginInfo = {
							loginname: accountBox.value,
							password: passwordBox.value,
							loginType:loginType,
							ctoken: clientInfo.token || '' ,
							clientid: clientInfo.clientid || '' ,
							appid: clientInfo.appid || '' ,
							appkey:clientInfo.appkey,
							debug:false
						};
						
						console.debug("=== loginInfo === " + url) ;
						login.dologin(url,loginInfo, function(err) {
							waiting&&waiting.close();
							waiting=null;
							if (err) {
								plus.nativeUI.toast(err);
								return;
							}
							
							toMain();
						});
					});
					$.enterfocus('#login-form input', function() {
						$.trigger(loginButton, 'tap');
					});
					 
					var backButtonPress = 0;
					
					loginTypePicker.setData(roles);
					loginTypeButton.addEventListener('tap', function(event) {
						loginTypePicker.show(function(items) {
							
							loginType = items[0].value ;
							loginTypeButton.innerText = items[0].text ;
							url = items[0].url ;
							console.debug(JSON.stringify(items[0])) ;
							
							//返回 false 可以阻止选择框的关闭
							//return false;
						});
					}, false);
					
				});
			}(mui, document));
			
			//处理逻辑：1秒内，连续两次按返回键，则退出应用；
//			var first = null;
//			mui.back = function() {
//				//首次按键，提示‘再按一次退出应用’
//				if (!first) {
//					first = new Date().getTime();
//					mui.toast('再按一次退出应用');
//					setTimeout(function() {
//						first = null;
//					}, 1000);
//				} else {
//					if (new Date().getTime() - first < 1000) {
//						plus.runtime.quit();
//					}
//				}
//				
//			};
		</script>
	</body>

</html>